<template>
  <div class="save-as">
    <a-modal
      v-model="visible"
      :title="false"
      @ok="handleOk"
      okText="确定"
      cancelText="取消"
      :ok-button-props="{ props: { loading: isLoading } }">
      <div class="form">
        <CommonForm ref="formRef" :onChange="handleChange" :formData="formData" :formJson="formJson" />
      </div>

    </a-modal>

  </div>
</template>

<script >

import { mapState } from "vuex"
import CommonForm from "../../../common/CommonForm/index2"
import EventBus from "../../EventBus"
import { getDashboardCategoryTree } from "@/api/multidimensionalAnalysis"
import { save } from "../../lib/save"

export default {
  data() {
    return {
      visible: false,
      isLoading: false,
      formJson: [
        {
          type: "input",
          label: "名称",
          fieldName: "name",
          initialValue: "",
          extraConfig: {
            className: "row",
          },
          rules: { required: true, message: "名称不能为空", trigger: "blur" }
        },
        {
          type: "tree-select",
          label: "位置",
          fieldName: "dasDashboardCategoryId",
          initialValue: "",
          extraConfig: {
            className: "row",
            treeData: [],
            replaceFields: {title: "name", value: "id"}
          },
          rules: { required: true, message: "位置不能为空", trigger: "change" }
        },
      ],
      formData: {
        name: "",
        dasDashboardCategoryId: ""
      }
    }
  },
  methods: {
    handleOk(){
      this.$refs.formRef.form.validate(valid => {
        if(valid){
          this.isLoading = true
          EventBus.$emit("saveAsSave", {
            ...this.formData
          })
          setTimeout(()=> {
            this.visible = false
            this.formData = {
              name: "",
              dasDashboardCategoryId: ""
            }
            this.isLoading = false
          }, 300)
        }
      })

    },
    handleChange(fieldName, value){

    }
  },
  computed: {
    ...mapState("lowCode", {
      pageSetting: state => state.pageSetting
    })
  },
  created() {
    // 获取保存位置
    getDashboardCategoryTree()
    .then(data => {
      // this.treeData = data.data || []
      this.formJson[1].extraConfig.treeData = data.data
    })
    EventBus.$oneOn("saveAs", ()=> {
      this.visible = true
    })
  },
  components: {
    CommonForm
  }
}
</script>

<style lang="less" scoped>
 @import url("./index.less");
</style>
